package com.naiterui.ehp.bs.cms.modules.followup.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.naiterui.ehp.bp.bo.emr.QaPageBO;
import com.naiterui.ehp.bp.bo.followup.DrFollowUpRecordFormBO;
import com.naiterui.ehp.bp.bo.followup.FollowUpFormBO;
import com.naiterui.ehp.bp.bo.form.FormDrawingBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.FileUtil;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.followup.service.IFollowUpFormService;
import com.naiterui.ehp.bs.cms.modules.followup.vo.FollowUpFormDetailVO;
import com.naiterui.ehp.bs.cms.modules.followup.vo.FollowUpFormVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yangfuya
 * @since 2022/1/5
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.FOLLOW_UP})
@RestController
@RequiredArgsConstructor
@RequestMapping("/followup/form")
public class FollowUpFormController {
    private final IFollowUpFormService followUpFormService;

    @SysLog("随访表单列表")
    @GetMapping("/list")
    @ApiOperation("随访表单列表")
    @PreAuthorize("@p.check('followup:form:list')")
    public PageVO<FollowUpFormVO> list(PageParamsVO pageParamsVO, FollowUpFormVO followUpFormVO) {
        return this.followUpFormService.queryPage(pageParamsVO, followUpFormVO);
    }

    @SysLog("随访表单保存修改")
    @PostMapping
    @ApiOperation("随访表单保存修改")
    @PreAuthorize("@p.check('followup:form:edit')")
    public void edit(@RequestBody FollowUpFormBO followUpFormBO) throws BusinessException {
        this.followUpFormService.edit(followUpFormBO);
    }

    @SysLog("随访表单详情")
    @GetMapping("/detail/{id}")
    @ApiOperation("随访表单详情")
    @PreAuthorize("@p.check('followup:form:detail')")
    public FollowUpFormDetailVO getById(@PathVariable Long id) throws BusinessException {
        return this.followUpFormService.getById(id);
    }

    @ApiOperation("随访表单文件上传")
    @PostMapping("/upload")
    @ApiImplicitParam(name = "file", value = "文件", dataType = "file", paramType = "formdate", required = true)
    @PreAuthorize("@p.check('followup:form:upload')")
    public ResponseVO<String> upload(@NotNull(message = "上传文件不能为null") MultipartFile file) throws BusinessException {
        String url = this.followUpFormService.upload(file);
        return PublicService.returnResponseVO(url);
    }

    @SysLog("随访表单删除")
    @DeleteMapping
    @ApiOperation("随访表单删除")
    @ApiImplicitParam(name = "id", value = "随访表单ID", dataType = "long", paramType = "query", required = true)
    @PreAuthorize("@p.check('followup:form:delete')")
    public void delete(@RequestParam Long id) throws BusinessException {
        this.followUpFormService.deleteById(id);
    }

    @SysLog("随访表单开启关闭")
    @PutMapping("/status/{id}/{status}")
    @ApiOperation("随访表单开启关闭")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "随访表单ID", dataType = "long", paramType = "path", required = true),
        @ApiImplicitParam(name = "status", value = "随访表状态", dataType = "int", paramType = "path", required = true)})
    @PreAuthorize("@p.check('followup:form:edit')")
    public void updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status)
            throws BusinessException {
        this.followUpFormService.updateStatus(id, status);
    }

    @SysLog("随访表单统计数据")
    @GetMapping("/{formId}")
    @ApiOperation("随访表单统计数据")
    @ApiImplicitParam(name = "formId", value = "随访表单ID", dataType = "string", paramType = "path", required = true)
    @PreAuthorize("@p.check('followup:form:detail')")
    public List<FormDrawingBO> view(@PathVariable("formId") String formId) throws BusinessException {
        return this.followUpFormService.getDesignForm(formId);
    }


    @SysLog("查询问卷统计列表")
    @ApiOperation("查询问卷统计列表")
    @GetMapping("/qa")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qaId", value = "表单列表中的表单id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "随访人名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fillingStatus", value = "填写状态 0.未填写 1.已填写 2全部", required = true, dataType = "String", paramType = "query")
    })
    @PreAuthorize("@p.check('followup:form:qa')")
    public QaPageBO qaRecord(Long qaId, String name, Integer fillingStatus, PageParamsVO pageParamsVO) throws BusinessException {
        return this.followUpFormService.qaRecord(qaId, name, fillingStatus, pageParamsVO);
    }

    @GetMapping("/qa/export")
    @ApiOperation("问卷导出API")
    @PreAuthorize("@p.check('followup:form:export')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qaId", value = "表单列表中的表单id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "随访人名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fillingStatus", value = "填写状态 0.未填写 1.已填写 2全部", required = true, dataType = "String", paramType = "query")
    })
    public void qaExport(Long qaId, String name, Integer fillingStatus, PageParamsVO pageParamsVO, HttpServletResponse response, HttpServletRequest request) throws BusinessException {
        byte[] body = this.followUpFormService.qaRecordExport(qaId, name, fillingStatus, pageParamsVO);
        String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
        FileUtil.downloadFile(request, response, FileUtil.writeBytes(body, fileName), true);
    }

    @SysLog("表单统计查看详情")
    @GetMapping("/qa/record/detail")
    @ApiOperation("表单统计查看详情")
    @PreAuthorize("@p.check('followup:form:qa:detail')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "followUpRecordFormId", value = "用户填写的问卷id", required = true, dataType = "long", paramType = "query")
    })
    public DrFollowUpRecordFormBO qaRecordDetail(@RequestParam Long followUpRecordFormId) throws BusinessException {
        return this.followUpFormService.qaRecordDetail(followUpRecordFormId);
    }
}
